﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using VaccineHost.Model;
using VaccineHost.Services;

namespace VaccineHost.Controllers
{
    [Route("api/v1/user")]
    [ApiController]
    public class UserController : ControllerBase
    {
        #region Option注入
        private readonly UserOption _userOptions;
        private readonly IJWTService _jWTService;
        public UserController(IOptionsMonitor<UserOption> userOptions, IJWTService jWTService)
        {
            this._userOptions = userOptions.CurrentValue;
            _jWTService = jWTService;
        }
        #endregion

        [Route("auth/{username}/{password}")]
        [HttpPost]
        public async Task<ActionResult<string>> Auth(string username,string password)
        {
            if (username == this._userOptions.UserName && password == this._userOptions.Password)
            {
                var token = _jWTService.GetToken(username);
                return Ok(token);
                //return Ok("登录成功");
            }
            else
            {
                return BadRequest("用户名或密码错误。");
            }
        }
    }
}
